// pages/leader/leader_withdraw.js
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    leader: {
      avatar_url: '',
      name: '',
      total_money: 0
    },
    takeMoney: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getMoney()
  },

  /**
   * 
   */
  moneyValue: function(e) {
    this.setData({
      takeMoney: e.detail.value
    })
  },

  /**
   * 获取团长可提现余额
   */
  getMoney: function() {
    let that = this,
        data
    app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
      wx.request({
        url: app.globalData.hostAPI + '/api/v1/rgl-cmdr/center?access_token=' + accessToken,
        success: res => {
          data = res.data.data
          if(res.data.status === 1) {
            that.setData({
              'leader.name': data.name,
              'leader.total_money': data.balance,
              'leader.avatar_url': data.avatar
            })
          }
        }
      })
    })
  },
  /**
   * 全部提现
   */

  takeAllMoney: function() {
    this.setData({
      takeMoney: this.data.leader.total_money
    })
  },

  /**
   * 
   */
  makeBalance: function() {
    let that = this

    if (that.data.takeMoney == '') {
      wx.showToast({
        icon: 'none',
        title: '请输入提现金额',
      })
      return false
    }

    if (that.data.takeMoney == 0) {
      wx.showToast({
        icon: 'none',
        title: '提现金额不能为0',
      })
      return false
    }

    if (that.data.takeMoney < 10) {
      wx.showToast({
        icon: 'none',
        title: '提现金额不能小于10元',
      })
      return false
    }

    if (that.data.takeMoney > that.data.leader.total_money) {
      wx.showToast({
        icon: 'none',
        title: '金额超出余额',
      })
      return false
    }

    wx.showModal({
      title: '提示',
      content: '是否确认提现到零钱',
      success: res => {
        if(res.confirm) {
          wx.showLoading({
            mask: true,
            title: app.globalData.loadText,
          })
          app.getAccessToken(app.getPageUrl(getCurrentPages())).then(function (accessToken) {
            wx.request({
              url: app.globalData.hostAPI + '/api/v1/rgl-cmdr/withdraw?access_token=' + accessToken,
              data: {
                amount: that.data.takeMoney
              },
              success: res => {
                if(res.data.status === 1) {
                  wx.hideLoading()
                  wx.showModal({
                    title: '提示',
                    content: '提现申请已提交'
                  })
                } else {
                  wx.hideLoading()
                  wx.showToast({
                    icon: 'none',
                    title: res.data.msg,
                  })
                }
              },
              fail: res => {
                wx.hideLoading()
                wx.showToast({
                  icon: 'none',
                  title: res.data.msg,
                })
              }
            })
          })
        }
      }
    })
    
  }
})